
#HLA EPITOPES FREQUENCY DATABASE
#Function: Insert data into [epit_frequency] table [EpitHLA] schema
#Created in: 29.01.2014
#Author: Louise Takeshita


use strict;
use EpiHash qw(epi_data);

#Process Epitope data into HashMap
my %epitopes = epi_data; # returns epitope hashmap

#Check for arguments in the command line
if(@ARGV != 2){
	die "Invalid number of arguments!\n";
}
chomp(@ARGV);
my $input = $ARGV[0];
my $pop_id = $ARGV[1];
my $pop = substr($input,0,-4);

#Create output file
open(OUTPUT,">insert_$pop.sql") || die "The file insert_$pop.sql could not be opened.\n"; #open file and handle unexpected errors

#Read File
open(INPUT,$input) || die "The file " . $input . " could not be opened.\n"; #open file and handle unexpected errors

#Processing
my $counter = 0;
while(my $line = <INPUT>)
	{
	if($counter != 0)
		{
		chomp($line);
		$line =~ s/^\s+//g; #remove leading spaces
		$line =~ s/\s+$//; #remove trailing spaces
		my @line = split(/,/,$line);
		my $epit_id = $epitopes{$line[1]}{'pos'};
		my $freq = sprintf("%.2f", $line[3]);
		print OUTPUT "INSERT INTO EpitHLA.epit_frequency VALUES ('$pop_id',$epit_id,$freq);\n";
		}
	$counter++;
	}

print "Done!";